export const titleObj = {
  Transaction: {
    powerIncome: {
      title: '发电收入',
      unit: '万元',
      hover: '#28D8FF',
      header: true,
      children: ['midIncome', 'spotIncome'],
      otherIncome: { title: '其他收入', unit: '万元', hover: '#28D8FF' },
    },
    internetQuantity: { title: '上网电量', unit: '万kWh', header: true, hover: '#4CEC82' },
    basicHours: { title: '基础小时数', unit: 'h', header: true, hover: '#FFE711' },
    basicQuantity: { title: '基础电量', unit: '万kWh', header: true, hover: '#4AF2F2' },
    collectPrice: { title: '综合电价', unit: '元/kWh' },
    dealPrice: { title: '交易电价', unit: '元/kWh' },
    midIncome: { title: '中长期', unShow: true, hover: '#28D8FF' },
    spotIncome: { title: '现货', unShow: true, hover: '#28D8FF' },
  },
  ElectricitySales: {
    signCapacity: { title: '签约用户电量', unit: '万kWh', header: true, hover: '#28D8FF' },
    executionCapacity: { title: '执行电量', unit: '万kWh', header: true, hover: '#4CEC82' },
    grossProfit: { title: '毛利润', unit: '万元', header: true, hover: '#FFE711' },
    deviationAssessmentCost: {
      title: '偏差考核费用',
      unit: '万元',
      deviation: true,
      header: true,
      hover: '#4AF2F2',
    },
    priceDifferenceIncome: { title: '价差收入', unit: '万元' },
    agencyFee: { title: '代理商费用', unit: '万元' },
  },
};

export const allName = '全部';

export const mapOption = (data, rightSelect, color, code, revenueAnalysis) => {
  let flag = false;
  let newData = data.map((item) => {
    if (item[rightSelect] < 0) {
      flag = true;
    }
    return {
      name: item.provinceName,
      value: item[rightSelect],
    };
  });
  let colorData = flag
    ? [...color[rightSelect], { max: 0, min: null, color: '#FA6767' }]
    : color[rightSelect];
  let tooltip = {};
  if (revenueAnalysis) {
    tooltip.backgroundColor = '#161A1D';
    tooltip.borderColor = '#161A1D';
    tooltip.textStyle = {
      color: '#A7A8A9',
    };
  }
  return {
    tooltip: {
      formatter: (params) => {
        if (params.data && !Number.isNaN(params.value)) {
          if (params.data.value || params.data.value === 0) {
            return `${params.name} : ${params.value}`;
          }
        }
        return params.name + '：-';
      },
      ...tooltip,
    },
    visualMap: [
      {
        type: 'piecewise',
        min: 0,
        max: 1000,
        left: 26,
        bottom: 40,
        showLabel: true,
        pieces: colorData,
        itemWidth: 12,
        itemHeight: 12,
        textStyle: {
          fontSize: 12,
          color: '#ACB1C1',
        },
        show: true,
      },
    ],
    series: [
      {
        name: '地图',
        type: 'map',
        mapType: 'china',
        label: {
          emphasis: {
            color: '#fff',
            fontSize: 9,
          },
        },
        itemStyle: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#fff',
          areaColor: '#fff',
        },
        emphasis: {
          itemStyle: {
            areaColor: titleObj[code][rightSelect].hover || '#eee',
            borderColor: 'rgba(0, 0, 0, 0.2)',
          },
        },
        data: newData,
      },
    ],
  };
};

export const rankingOption = (data, rightSelect, colorData, revenueAnalysis) => {
  let flag = false;
  let newData = data
    .map((item) => {
      let obj = {
        name: item.provinceName,
        value: item[rightSelect],
      };
      if (obj.value < 0) {
        flag = true;
      }
      return obj;
    })
    .sort((a, b) => b.value - a.value)
    .filter((n) => n.name !== allName && n.value !== null);
  let pieces = flag
    ? [...colorData[rightSelect], { max: 0, min: null, color: '#FA6767' }]
    : colorData[rightSelect];

  return {
    grid: {
      top: 0,
      bottom: 0,
      left: 50,
      right: 80,
      // containLabel: true,
    },
    visualMap: [
      {
        type: 'piecewise',
        pieces,
        dimension: 0,
        show: false,
      },
    ],
    xAxis: {
      show: false,
      type: 'value',
    },
    yAxis: [
      {
        type: 'category',
        inverse: true,
        splitLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
        axisLine: {
          show: flag,
          lineStyle: {
            width: 0.5,
            color: '#D8DCE6',
          },
        },
        axisLabel: {
          interval: 0,
          // margin:0,
          // align: 'left',
          textStyle: {
            color: revenueAnalysis ? '#fff' : '#000',
          },
          formatter: (item) => {
            if (item.length === 2) {
              return item + '   ';
            }
            return item;
          },
        },
        data: newData.map((n) => n.name),
      },
      {
        type: 'category',
        inverse: true,
        axisTick: 'none',
        axisLine: 'none',
        show: true,
        axisLabel: {
          align: 'right',
          margin: 70,
          // backgroundColor: '#f00',
          textStyle: {
            color: revenueAnalysis ? '#fff' : '#000',
            fontSize: '12',
            interval: 0,
          },
        },
        data: newData.map((n) => n.value),
      },
    ],
    series: [
      {
        type: 'bar',
        itemStyle: {
          normal: {
            color: 'red',
            barBorderRadius: 15,
          },
        },
        barMaxWidth: 8,
        data: newData.map((n) => n.value),
      },
    ],
  };
};
